package net.bingosoft.common.struts;


import java.util.Hashtable;
import java.util.Map;

import net.bingosoft.common.util.LimitUtil;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.struts.validator.ValidatorForm;
import org.ecside.table.limit.Limit;

/**
 * 所有分页查询时的form基类
 *
 */
public class BaseActionForm extends ValidatorForm {
	private static final long serialVersionUID = 1L;
	private int pageSize= 0;
	private Limit limit;
	private String orderBy = "";
	
	public BaseActionForm(){
		super();
	}

	//other query params 
	private Map<String ,Object> params = new Hashtable<String,Object>();

	/**
	 * 添加一个查询条件,在ibatis中引用的方式为 #params.key# 或 $params.key$
	 * @param key
	 * @param value
	 */
	public void addParam(String key,Object value){
		params.put(key, value);
	}
	/**
	 * 删除一个参数
	 * @param key
	 * @param value
	 */
	public void removeParam(String key){
		params.remove(key);
	}
	/**
	 * 获取查询参数
	 * @return
	 */
	public Map<String ,Object> getParams() {
		return params;
	}
	/**
	 * 获取Limit对象用于分页
	 * @return
	 */
	public Limit getLimit() {
		return limit;
	}
	/**
	 * 设置Limit
	 * @param limit
	 */
	public final void setLimit(Limit limit) {
		this.limit = limit;
		this.orderBy = LimitUtil.getOrderBy(limit);
	}
	/**
	 * 获取排序字段,应该在 setLimit 之后调用
	 * @return
	 */
	public String getOrderBy() {
		return orderBy;
	}
	/**
	 * 获取排序的属性
	 * @return
	 */
	public String getOrderProperty(){
		return orderBy.replaceFirst("order by", "");
	}
	/**
	 * 获取分页大小
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * 设置分页大小
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 * 设置参数表
	 * @param params
	 */
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	/**
	 * 
	 */
	@Override
	public String toString(){
		String result = null;
		try{
			result = ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
		}catch(Exception ex){
			result = super.toString();
		}
		return result;
	}

}
